﻿using Ekton_Manage_DomainCore;
using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Product;
using Ekton_Manage_DTO.RequestModels.User;
using Ekton_Manage_DTO.RequestModels.View;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_Framework.Redis;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using Ekton_Manage_Mapper.EntityMappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class SelectProductList : ISelectProductList
    {
        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<product, int> _baseproductRepository;
        private readonly IBaseRepository<product_type, int> _baseMaterialRepository;
        private readonly ApplicationDbContent _content;
        public SelectProductList(SerializeParameter serialize, IBaseRepository<product, int> baseproductRepository, IBaseRepository<product_type, int> baseMaterialRepository, ApplicationDbContent content)
        {
            _serialize = serialize;
            _baseproductRepository = baseproductRepository;
            _baseMaterialRepository = baseMaterialRepository;
            _content = content;
        }
        /// <summary>
        ///  查询产品列表
        /// </summary>
        /// 
        public async Task<Tuple<List<product_view>, int>> GetProductList(QueryDataParame query)
        {
            var GetProductList = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<product_view, bool>> expression = m => true;
            if (GetProductList.ContainsKey("product_name") && !string.IsNullOrEmpty(GetProductList["product_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.product_name.Contains(GetProductList["product_name"].ToString()));
            }
            if (GetProductList.ContainsKey("product_brand") && !string.IsNullOrEmpty(GetProductList["product_brand"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.product_brand.Contains(GetProductList["product_brand"].ToString()));
            }
            if (GetProductList.ContainsKey("rank") && !string.IsNullOrEmpty(GetProductList["rank"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.rank.Contains(GetProductList["rank"].ToString()));
            }
            var List = _content.Set<product_view>().Where(expression)
                               .OrderByDescending(m => m.id)
                               .Skip((query.pageIndex - 1) * query.pageSize)
                               .Take(query.pageSize)
                               .ToList();
            int total = _content.Set<product_view>().Where(expression).Count();
            return Tuple.Create<List<product_view>, int>(List, total);
        }

        /// <summary>
        /// 编辑产品列表
        /// </summary>
        /// <param name="requset"></param>
        /// <returns></returns>
        public async Task<Message> PutProductList(DtoInsertProductList requset)
        {
            var sysProductList = await _baseproductRepository.QuerySingleData(requset.id);
            if (sysProductList == null) return new Message(false, 202);
            sysProductList.product_picture = requset.product_picture;
            sysProductList.product_name = requset.product_name;
            sysProductList.materialItem = requset.materialItem;
            sysProductList.design = requset.design;
            sysProductList.brand_id = requset.brand_id;
            sysProductList.classify_id = requset.classify_id;
            sysProductList.remark = requset.remark;
            sysProductList.materials_id = requset.materials_id;
            sysProductList.product_describe = requset.product_describe;
            var result = await _baseproductRepository.UpdateSingleData(sysProductList);
            return new Message(result.Item1);
        }

        /// <summary>
        /// 添加产品列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Message> PostProductList(DtoInsertProductList request)
        {
            var list = await _baseproductRepository.InsertSingleData(new product
            {
                product_name = request.product_name,
                product_picture = request.product_picture,
                product_describe = request.product_describe,
                remark = request.remark,
                brand_id = request.brand_id,//品牌id
                classify_id = request.classify_id,//产品分类id
                materials_id = request.materials_id,//材料id
                materialItem = request.materialItem,
                design = request.design
            });
            return new Message(list.Item1);
        }

        /// <summary>
        /// 删除产品列表
        /// </summary>
        /// <returns></returns>
        public async Task<Message> DeleteProductList(int id)
        {
            var list = await _baseproductRepository.DeleteSingleData(id);
            return new Message(list);
        }

        /// <summary>
        ///  查询产品分类
        /// </summary>
        /// 
        public async Task<Tuple<List<product_type>, int>> SelectProductClassList(QueryDataParame query)
        {
            var dicData = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<product_type, bool>> expression = m => true;
            if (dicData.ContainsKey("class_name") && !string.IsNullOrEmpty(dicData["class_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.class_name.Contains(dicData["class_name"].ToString()));
            }

            if (dicData.ContainsKey("father_name") && !string.IsNullOrEmpty(dicData["father_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.father_name.Contains(dicData["father_name"].ToString()));
            }

            if (dicData.ContainsKey("rank") && !string.IsNullOrEmpty(dicData["rank"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.rank.Contains(dicData["rank"].ToString()));
            }

            return await _baseMaterialRepository.QueryPageData(query, expression);

        }

        /// <summary>
        ///  编辑产品分类
        /// </summary>
        /// 
        public async Task<Message> PutProductClass(DtoSelectProductList query)
        {
            var userInfo = await _baseMaterialRepository.QuerySingleData(query.id);
            if (userInfo == null) return new Message(false, 202);
            userInfo.product_describe = query.product_describe;
            userInfo.class_name = query.class_name;
            userInfo.product_picture = query.product_picture;
            userInfo.rank = query.rank;
            userInfo.father_name = query.father_name;
            userInfo.father_id = query.father_id;
            var result = await _baseMaterialRepository.UpdateSingleData(userInfo);
            return new Message(result.Item1);
        }

        /// <summary>
        /// 添加产品分类
        /// </summary>
        /// <returns></returns>
        public async Task<Message> PostProductClass(DtoSelectProductList prouderType)
        {
            var userInfo = await _baseMaterialRepository.InsertSingleData(new product_type
            {
                product_describe = prouderType.product_describe,
                class_name = prouderType.class_name,
                product_picture = prouderType.product_picture,
                father_name = prouderType.father_name,
                rank = prouderType.rank
            });
            return new Message(userInfo.Item1);
        }

        /// <summary>
        /// 删除产品分类
        /// </summary>
        /// <returns></returns>
        public async Task<Message> DeleteProductTypeList(int id)
        {
            var list = await _baseMaterialRepository.DeleteSingleData(id);
            return new Message(list);
        }

    }
}
